package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.ResponseModel;
import io.renren.modules.sys.entity.SysMenuEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.vo.PhoneVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.service.UserService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-07-06 10:14:25
 */
@RestController
@RequestMapping("sys/tuser")
@Api(tags = "后台管理-注册用户")
public class TUserController extends AbstractController {
    @Autowired
    private UserService userService;


    @PostMapping("/checkPhones")
    @ApiOperation("检测手机号是否已经存在")
    public ResponseModel<List<PhoneVo>> checkPhones(@RequestBody List<String> mobiles) {
        List<PhoneVo> list = userService.checkPhones(mobiles);
        return ResponseModel.of(list);
    }

    @PostMapping("/batch")
    @ApiOperation("批量保存客户")
    public ResponseModel<Boolean> batch(@RequestBody List<UserEntity> lists) {
        Long userId = super.getUserId();
        boolean rtn = userService.batch(lists, userId);
        return ResponseModel.of(rtn);
    }


    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("app:user:list")
    @ApiOperation("注册用户列表")
    public ResponseModel<PageUtils> list(@RequestParam Map<String, Object> params){
        SysUserEntity sysUserEntity = super.getUser();
        PageUtils page = userService.queryPage(params, sysUserEntity);

        return ResponseModel.of(page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{userId}")
    @RequiresPermissions("app:user:info")
    @ApiOperation("注册用户详情")
    public ResponseModel<UserEntity> info(@PathVariable("userId") Long userId){
		UserEntity user = userService.getUserInfoId(userId);

        return ResponseModel.of(user);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("app:user:save")
    @ApiOperation("新增注册用户")
    public R save(@RequestBody UserEntity user){
        user.setCreateUserId(getUserId());
        user.setCreateTime(new Date());
        user.setUpdateUserId(getUserId());
        user.setUpdateTime(new Date());
		userService.saveUser(user);

        return R.ok();
    }

    @SysLog("修改客户状态")
    @PostMapping("/updateState")
    @RequiresPermissions("app:user:update")
    @ApiOperation("修改客户状态")
    public ResponseModel<Boolean> updateState(@RequestBody UserEntity entity) {
        entity.setUpdateUserId(getUserId());
        entity.setUpdateTime(new Date());
        boolean isSuccess = userService.updateState(entity);
        return ResponseModel.of(isSuccess);
    }


    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("app:user:update")
    @ApiOperation("修改注册用户")
    public R update(@RequestBody UserEntity user){
        user.setUpdateUserId(getUserId());
        user.setUpdateTime(new Date());
		userService.update(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("app:user:delete")
    @ApiOperation("删除注册用户")
    public R delete(@RequestBody Long[] userIds){
		userService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

    @PostMapping("/upload")
    @ApiOperation("导入")
    public ResponseModel<Boolean> upload(@RequestParam("file") MultipartFile file, @RequestParam Map<String, String> params){
//        String msg = userService.upload(file, params);
        return null;
    }
}
